package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.CommonUser;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.JaveTeacher;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityMyInfoBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.CommonUserDao;
import com.example.administrator.familfinalcialmanagement.greendao.gen.JaveTeacherDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;

/**
 * Created by Administrator on 2019/3/26.
 */

public class MyInfoActivity extends BaseActivity {

    ActivityMyInfoBinding binding;
    CommonUserDao commonUserDao = GreenDaoUtils.getSingleTon().getmDaoSession().getCommonUserDao();
    CommonUser user;
    String isLogin;
    boolean isPoint = true;
    String pwdStatus;

    JaveTeacherDao teacherDao = GreenDaoUtils.getSingleTon().getmDaoSession().getJaveTeacherDao();
    JaveTeacher javeTeacher;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_info);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        setBack();

        String intentPhone = getIntent().getStringExtra("module_phone");
        String intentPwd = getIntent().getStringExtra("module_pwd");
        isLogin = (String) SpUtils.get("isLogin", "");
        String phone = (String) SpUtils.get("module_phone", "");
        String pwd = (String) SpUtils.get("module_pwd", "");
        LogUtil.e("phone-->" + phone);
        LogUtil.e("pwd-->" + pwd);
        if (!TextUtils.isEmpty(intentPhone)) {
            for (CommonUser user : commonUserDao.loadAll()) {
                if (intentPhone.equals(user.getPhone()) && intentPwd.equals(user.getPwd())) {
                    binding.setBean(user);
                    this.user = user;
                }
            }
        } else {
            for (CommonUser user : commonUserDao.loadAll()) {
                if (phone.equals(user.getPhone()) && pwd.equals(user.getPwd())) {
                    binding.setBean(user);
                    this.user = user;
                }
            }
        }


        /**
         * isLogin
         *      1  超级管理员
         *      2  项目管理者
         *      3  普通人
         * commonUser
         *       pwdStatus
         *       1 锁定
         *       2 非锁定
         * tvModulePwdStatus
         *       界面显示密码状态的控件
         */

        if (user == null) {
            ToastUtil.showToast("查无此人");
            return;
        }

        if ("1".equals(user.pwdStatus)) {
            binding.tvModulePwdStatus.setText("锁定");
            binding.llModulePwdClicked.setVisibility(View.VISIBLE);
            binding.llModulePwdUnClock.setVisibility(View.GONE);
        } else {
            binding.tvModulePwdStatus.setText("非锁定");
            binding.llModulePwdClicked.setVisibility(View.GONE);
            binding.llModulePwdUnClock.setVisibility(View.VISIBLE);
        }
        pwdStatus = user.pwdStatus;
        switch (isLogin) {
            case "1":
                LogUtil.e("超管");
                binding.tvModulePwdStatus.setOnClickListener(this);
                binding.llModulePwdClicked.setVisibility(View.GONE);
                binding.llModulePwdUnClock.setVisibility(View.VISIBLE);
                break;
            case "2":
                LogUtil.e("项目管理者");
                for (JaveTeacher teacher : teacherDao.loadAll()) {
                    if (phone.equals(teacher.getPhone())) {
                        javeTeacher = teacher;
                        LogUtil.e("老师教-->" + teacher.getClassName());
                    }
                }

                /**
                 * 根据老师的课程隐藏其他课程分数
                 */
                switch (javeTeacher.getClassName()){
                    case "JAVA":
                        binding.llModuleClassOneName.setVisibility(View.VISIBLE);
                        binding.llModuleClassOneNum.setVisibility(View.VISIBLE);
                        binding.llModuleClassNameTwo.setVisibility(View.GONE);
                        binding.llModuleClassTwoNum.setVisibility(View.GONE);
                        binding.llModuleClassNameThree.setVisibility(View.GONE);
                        binding.llModuleClassThreeNum.setVisibility(View.GONE);
                        break;
                    case "C":
                        binding.llModuleClassOneName.setVisibility(View.GONE);
                        binding.llModuleClassOneNum.setVisibility(View.GONE);
                        binding.llModuleClassNameTwo.setVisibility(View.GONE);
                        binding.llModuleClassTwoNum.setVisibility(View.GONE);
                        binding.llModuleClassNameThree.setVisibility(View.VISIBLE);
                        binding.llModuleClassThreeNum.setVisibility(View.VISIBLE);
                        break;
                    case "PHP":
                        binding.llModuleClassOneName.setVisibility(View.GONE);
                        binding.llModuleClassOneNum.setVisibility(View.GONE);
                        binding.llModuleClassNameTwo.setVisibility(View.VISIBLE);
                        binding.llModuleClassTwoNum.setVisibility(View.VISIBLE);
                        binding.llModuleClassNameThree.setVisibility(View.GONE);
                        binding.llModuleClassThreeNum.setVisibility(View.GONE);
                        break;
                        default:
                }
                break;
            case "3":
                LogUtil.e("普通人");
                break;
            default:
        }

        binding.tvModuleUp.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvModulePwdStatus:
                if (isPoint) {
                    isPoint = false;
                    if ("1".equals(pwdStatus)) {
                        pwdStatus = "2";
                        binding.tvModulePwdStatus.setText("非锁定");
                    } else {
                        pwdStatus = "1";
                        binding.tvModulePwdStatus.setText("锁定");
                    }
                } else {
                    isPoint = true;
                    if ("1".equals(pwdStatus)) {
                        pwdStatus = "2";
                        binding.tvModulePwdStatus.setText("非锁定");
                    } else {
                        pwdStatus = "1";
                        binding.tvModulePwdStatus.setText("锁定");
                    }
                }
                break;
            case R.id.tvModuleUp:
                String name = binding.tvModuleName.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.showToast("请输入名字");
                    return;
                }

                String phone = binding.tvModulePhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入账号");
                    return;
                }

                String pwd;
                if (pwdStatus.equals("2")) {
                    pwd = binding.etModulePwd.getText().toString().trim();
                } else {
                    pwd = user.getPwd();
                }

                String classNameOne = binding.tvModuleClassOneName.getText().toString().trim();
                String classNameTwo = binding.tvModuleClassNameTwo.getText().toString().trim();
                String classNameThree = binding.tvModuleClassNameThree.getText().toString().trim();

                String classOneNum = binding.etModuleClassOneNum.getText().toString().trim();
                if (TextUtils.isEmpty(classOneNum)) {
                    ToastUtil.showToast("请输入科目一分数");
                    return;
                }

                String classTwoNum = binding.etModuleClassTwoNum.getText().toString().trim();
                if (TextUtils.isEmpty(classTwoNum)) {
                    ToastUtil.showToast("请输入科目二分数");
                    return;
                }

                String classThreeNum = binding.etModuleClassThreeNum.getText().toString().trim();
                if (TextUtils.isEmpty(classThreeNum)) {
                    ToastUtil.showToast("请输入科目三分数");
                    return;
                }

                CommonUser commonUser = commonUserDao.queryBuilder().where(CommonUserDao.Properties.Phone.eq(phone)).build().unique();
                commonUser.setName(name);
                commonUser.setPwdStatus(pwdStatus);
                commonUser.setPwd(pwd);
                commonUser.setPhone(phone);
                commonUser.setClassNameOne(classNameOne);
                commonUser.setClassOneNum(classOneNum);
                commonUser.setClassNameTwo(classNameTwo);
                commonUser.setClassTwoNum(classTwoNum);
                commonUser.setClassNameThree(classNameThree);
                commonUser.setClassThreeNum(classThreeNum);
                commonUserDao.update(commonUser);
                ToastUtil.showToast("修改完毕");
                finish();
                break;
            default:
        }
    }
}
